#ifndef _USER_H
#define _USER_H
#include"clientHeadFile/user.h"
#endif

#ifndef _HEAD_H
#define _HEAD_H
#include<my_head.h>
#endif

#ifndef _MES_H
#define _MES_H
#include"clientHeadFile/mes.h"
#endif

#ifndef _UTILS_H
#define _UTILS_H
#include"clientHeadFile/utils.h"
#endif

#define CONFIGPATH "./config"

static int getparameter(char *key, char *value)
{
    FILE * file = fopen("conf", "r");
    while(1)
    {
        char line[100];
        bzero(line, sizeof(line));
        // 读一行数据
        char *res = fgets(line, sizeof(line), file);
        if(res == NULL)
        {
            char buf[] = "没有要找的内容 \n";
            memcpy(value, buf, strlen(buf));
            return -1;
        }
        // 处理数据
        char *line_key = strtok(line, "=");
        if(strcmp(key, line_key) == 0)
        {
            // 要找的内容
            char *line_value = strtok(NULL, "=");
            memcpy(value, line_value, strlen(line_value));
            return 0;
        }
    }
    return 0;
}
static bool readConfig()
{
    return false;
}
bool login(User * me , int socketFd)
{
    memset(me , 0 , sizeof(*(me)));
    if(!readConfig())//获取本地配置
    {
        printf("请输入用户名:");
        fflush(stdout);
        scanf("%s" , &(me->account));
        printf("请输入密码:");
        fflush(stdout);
        scanf("%s" , &(me->password));
    }

    //姓名
    strcat(me->name , "SB");


    //设置我的状态
    me->stat = 1;
    //设置当前路径
    strcat(me->currPath , me->account);
    strcat(me->currPath , "/");

    printf("user.c/ 76: currPath is %s , strlen(currPath) is %ld\n"
           , me->currPath , strlen(me->currPath));


    userMes mes;

    mes.user = *(me);
    mes.mesType = LOGIN;
    mes.cType = noControl;
    mes.contentSize = 0;
    
    printf("发送了%ld\n" , sizeof(mes) - MAX_BUF_FILE);


    send(socketFd , &mes ,  sizeof(mes) - MAX_BUF_FILE , 0);

    recvMes recvM;
    memset(&recvM , 0 , sizeof(recvM));
    int recv_flag = recv(socketFd , &recvM , sizeof(recvM) - MAX_BUF_FILE ,0);
    printf("recv_flag is %d ,contentSize is %d , sizeof(recM) - MAX_BUF is %ld \n"
           ,recv_flag , recvM.contentSize , sizeof(recvM) - MAX_BUF_FILE
           );
    if(recvM.contentSize != 0)recv(socketFd , recvM.content , recvM.contentSize , 0);
    
    if(recv_flag <= 0)return false;
    if(recvM.rStat != YES)return false;

    return true;
}

bool signup(int sofd)
{
    User me;
    memset(&me , 0 , sizeof(me));

    printf("请输入账号:");
    fflush(stdout);
    scanf("%s" , &(me.account));
    printf("请输入密码:");
    fflush(stdout);
    scanf("%s" , &(me.password));

    
    me.stat = 1;
    strcat(me.currPath , me.account);
    strcat(me.currPath , "/");
    

    printf("%ld , %s\n" , strlen(me.currPath) , me.currPath);
    

    userMes umes;
    memset(&umes , 0 , sizeof(umes));

    umes.mesType = SIGNUP;
    umes.cType = noControl;
    umes.user = me;
    umes.allFileSize = 0;
    umes.contentSize = 0;

    int sign_ret = send(sofd , &umes , sizeof(umes) - MAX_BUF_FILE , 0);
    if(sign_ret <= 0)
    {
        printf("服务器出现问题\n");
        return false;
    }


    recvMes recvM;
    printf("请等待服务器注册....\n");
    sleep(3);

    int recv_flag = recv(sofd , &recvM , sizeof(recvM) - MAX_BUF_FILE , MSG_DONTWAIT);
    if(recv_flag == -1){return false;}
    if(recvM.contentSize != 0)recv(sofd , recvM.content , recvM.contentSize , 0);
    if(!(recvM.rStat == YES && recvM.cType == noControl))return false;
    return true;
}
